<?php
	/** SNS TWITTER類
	*	Date: 	2012-5-18
	*	Author:	merlin
	*/
	class SNS_TWITTER {
		
		//TWITTER sdk
		private $_sdk;

		//授權后的囘調地址
		private $redirect_uri;

		//session操作類
		private $sess_obj;

		public function __construct(array $params) {
			$this->_sdk = $params['sdk'];
			$this->redirect_uri = $params['redirect_uri'];
			$this->sess_obj = $params['sess_obj'];
		}
		
		/** 獲取titter登入url */
		public function getLoginUrl($x_auth_access_type = null) {

			$params = array(
				'oauth_callback' => $this->redirect_uri
			);
			in_array($x_auth_access_type, array('write', 'read')) && $params['x_auth_access_type'] = $x_auth_access_type;
			$code = $this->_sdk->request('GET', $this->_sdk->url('oauth/request_token', ''), $params);
			$authurl = null;
			if ($code == 200) {
				$request_token = $this->_sdk->extract_params($this->_sdk->response['response']);
				$this->sess_obj->set('SNS_TRITTER_OAUTH_TOKEN', $request_token['oauth_token']);
				$this->sess_obj->set('SNS_TRITTER_OAUTH_TOKEN_SECRET', $request_token['oauth_token_secret']);
				$method = isset($_REQUEST['authenticate']) ? 'authenticate' : 'authorize';
				$force  = isset($_REQUEST['force']) ? '&force_login=1' : '';
				$authurl = $this->_sdk->url("oauth/{$method}", '') .  "?oauth_token={$request_token['oauth_token']}{$force}";
			}
			return $authurl;
		
		}
		
		/** 獲取weibo登出url */
		public function getLogoutUrl() {
			
		}

		/** 獲取用戶信息 */
		public function getUserInfo() {
			$user_info = null;
			$code = $this->_sdk->request('GET', $this->_sdk->url('1/account/verify_credentials'));
			return $code == 200 ? json_decode($this->_sdk->response['response'], true) : null;
		}

		/**
		 * 獲取主頁時間線
		 *
		 * @access public
		 * @param int $page 指定返回结果的页码。根据当前登录用户所关注的用户数及这些被关注用户发表的微博数，翻页功能最多能查看的总记录数会有所不同，通常最多能查看1000条左右。默认值1。可选。
		 * @param int $count 每次返回的记录数。缺省值50，最大值200。可选。
		 * @param int $since_id 若指定此参数，则只返回ID比since_id大的微博消息（即比since_id发表时间晚的微博消息）。可选。
		 * @param int $max_id 若指定此参数，则返回ID小于或等于max_id的微博消息。可选。
			......
		 * @return array
		 */
		public function getHomeTimeline($page = 1, $count = 50, $since_id = null, $max_id = null, $trim_user = null, $include_rts  = null, $include_entities  = null, $exclude_replies = null, $contributor_details = null) {

			$params = array(
				'page' => $page,
				'count' => $count
			);
			isset($since_id) && $params['since_id'] = $since_id;
			isset($max_id) && $params['max_id'] = $max_id;
			isset($trim_user) && $params['trim_user'] = $trim_user;
			isset($include_rts) && $params['include_rts'] = $include_rts;
			isset($include_entities) && $params['include_entities'] = $include_entities;
			isset($exclude_replies) && $params['exclude_replies'] = $exclude_replies;
			isset($contributor_details) && $params['since_id'] = $contributor_details;
			
			$code = $this->_sdk->request('GET', $this->_sdk->url('1/statuses/home_timeline'), $params);
			return $code == 200 ? json_decode($this->_sdk->response['response'], true) : null;

		}

		/**
		 * 獲取單條時間線
		 *
		 * @param int $id 
		 * @return array
		 */
		public function getTimeline($id, $trim_user = null, $include_entities = null, $include_my_retweet = null) {
			$params = array('id' => $id);
			isset($include_entities) && $params['include_entities'] = $include_entities;
			isset($trim_user) && $params['trim_user'] = $trim_user;
			$code = $this->_sdk->request('GET', $this->_sdk->url('1/statuses/show'), $params);
			return $code == 200 ? json_decode($this->_sdk->response['response'], true) : null;
		}

		/**
		 * 發表一條timeline(微博)
		 *
		 * @param string $content 內容
		 * @param int $pic_path 圖片路徑
		 * @param float $lat 緯度
		 * @param float $long 經度
		 * @return array
		 */
		public function addTimeline($content, $pic_path = null, $lat = NULL, $long = NULL, $place_id = NULL, $display_coordinates = NULL, $trim_user = null, $include_entities = null, $possibly_sensitive = null) {

			$params = array('status' => $content);
			isset($lat) && $params['lat'] = $lat;
			isset($long) && $params['long'] = $long;
			isset($place_id) && $params['place_id'] = $place_id;
			isset($display_coordinates) && $params['display_coordinates'] = $display_coordinates;
			$timeline = null;
			if ($pic_path) {
				$params['media[]'] = "@{$pic_path}";
				isset($possibly_sensitive) && $params['possibly_sensitive'] = $possibly_sensitive;
				$code = $this->_sdk->request('POST', 'https://upload.twitter.com/1/statuses/update_with_media.json', $params, true, true);
			} else {
				isset($trim_user) && $params['trim_user'] = $trim_user;
				isset($include_entities) && $params['include_entities'] = $include_entities;
				$code = $this->_sdk->request('POST', $this->_sdk->url('1/statuses/update'), $params);
			}
			
			return $code == 200 ? json_decode($this->_sdk->response['response'], true) : null;

		}

		/**
		 * 刪除一條timeline(微博)
		 *
		 * @param string $id timeline id
		 * @return array
		 */
		public function delTimeline($id, $include_entities = null, $trim_user = null) {
			$params = array('id' => $id);
			isset($include_entities) && $params['include_entities'] = $include_entities;
			isset($trim_user) && $params['trim_user'] = $trim_user;
			$code = $this->_sdk->request('POST', $this->_sdk->url('1/statuses/destroy'), $params);
			return $code == 200 ? json_decode($this->_sdk->response['response'], true) : null;
		}


	}

?>